@import "sass-list-maps";

// Transform the gutter property into a padding property for web
@function add-padding($breakpoints) {
  $cleanBreakpoints: ();
  @each $name, $breakpoint in $breakpoints {
    $cleanBreakpoints: map-merge(
      $cleanBreakpoints,
      (
        $name:
          map-merge($breakpoint, (padding: map-get($breakpoint, gutter) / 2))
      )
    );
  }

  @return $cleanBreakpoints;
}

// Merge standard and custom breakpoints into list
@function all-breakpoints($breakpoints, $extraBreakpoints, $first, $last) {
  $allBreakpoints: $breakpoints;
  @each $currentBreakpoint in $extraBreakpoints {
    $extraBreakpointName: nth($currentBreakpoint, 1);
    $extraBreakpointWidth: nth($currentBreakpoint, 2);
    $found: false;
    $match: null;
    @each $majorBreakpoint in $breakpoints {
      @if $found == false {
        @if map-get(nth($majorBreakpoint, 2), breakpoint) >
          $extraBreakpointWidth
        {
          $found: true;
        } @else {
          $match: $majorBreakpoint;
        }
      }
    }
    @if $extraBreakpointWidth > map-get($last, breakpoint) {
      $match: (
        last,
        $last
      );
    }
    @if $extraBreakpointWidth < map-get($first, breakpoint) {
      $match: (
        first,
        $first
      );
    }

    $newBreakpoint: map-merge(
      nth($match, 2),
      (breakpoint: $extraBreakpointWidth)
    );
    $currentBreakpoint: (
      $extraBreakpointName:
        map-merge(nth($match, 2), (breakpoint: $extraBreakpointWidth))
    );
    $allBreakpoints: map-merge($allBreakpoints, $currentBreakpoint);
  }

  @return maps-sort($allBreakpoints, breakpoint);
}

@function is-same-breakpoint($a, $b) {
  @return map-get($a, breakpoint) == map-get($b, breakpoint);
}

/// Traverse maps and retrieve deeply nested values: https://css-tricks.com/snippets/sass/deep-getset-maps/
/// @author Hugo Giraudel
/// @param {Map} $map A sass map and any number of keys.
/// @param {*} $keys Nested values.
/// @return Nested values or nested map.
@function map-deep-get($map, $keys...) {
  @each $key in $keys {
    $map: map-get($map, $key);
  }
  @return $map;
}

// _decimal.scss | MIT License | gist.github.com/terkel/4373420
@function round-decimal($number, $digits: 0, $mode: round) {
  $n: 1;
  // $number must be a number
  @if type-of($number) != number {
    @warn "#{ $number } is not a number.";
    @return $number;
  }
  // $digits must be a unitless number
  @if type-of($digits) != number {
    @warn "#{ $digits } is not a number.";
    @return $number;
  } @else if not unitless($digits) {
    @warn "#{ $digits } has a unit.";
    @return $number;
  }
  @for $i from 1 through $digits {
    $n: $n * 10;
  }
  @if $mode == round {
    @return round($number * $n) / $n;
  } @else if $mode == ceil {
    @return ceil($number * $n) / $n;
  } @else if $mode == floor {
    @return floor($number * $n) / $n;
  } @else {
    @warn "#{ $mode } is undefined keyword.";
    @return $number;
  }
}
